package com.qfedu.service.impl;

import com.qfedu.dao.UserMapper;
import com.qfedu.pojo.User;
import com.qfedu.service.UserService;
import com.qfedu.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    private Object User;

    @Override
    public List<com.qfedu.pojo.User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public BaseResult login(com.qfedu.pojo.User user, HttpSession session) {
 BaseResult baseResult= new BaseResult();

 String tel=user.getTel();
 String password=user.getPassword();
 com.qfedu.pojo.User loginUser =userMapper.findUserByTel(tel);

 if (loginUser==null){
     baseResult.setCode(500);
     baseResult.setMessage("忒！！！你还不是我们的人。。。快去注册！！！");
     return baseResult;
 }if (!password.equals(loginUser.getPassword())){
     baseResult.setCode(501);
     baseResult.setMessage("不是吧 还有人忘记密码？？？");
     return baseResult;

        }
  session.setAttribute("user",loginUser);
 session.setMaxInactiveInterval(60*60);
 baseResult.setCode(200);
 baseResult.setMessage("登录成功");

        return baseResult;
    }

    @Override
    public BaseResult add(com.qfedu.pojo.User user, HttpSession session) {

        List<com.qfedu.pojo.User> register = userMapper.register(user.getTel());

        BaseResult baseResult = new BaseResult();

String captcha= (String) session.getAttribute("captcha");
if (!captcha.equalsIgnoreCase(user.getCode())){
     baseResult.setCode(500);
     baseResult.setMessage("验证码错误");
     return  baseResult;
}if (register.size()>0 && register!=null){
    baseResult.setCode(500);
    baseResult.setMessage("用户名重复");
    return  baseResult;

        }
userMapper.add(user);
baseResult.setCode(200);
baseResult.setMessage("注册ok");
        return baseResult;
    }
    //修改

    @Override
    public BaseResult update(String tel, HttpSession session) {
 com.qfedu.pojo.User user = (com.qfedu.pojo.User) session.getAttribute("user");
 Integer id=user.getId();
        int i = userMapper.updateMin(tel, id);
        com.qfedu.pojo.User user1 = userMapper.selectById(id);
        BaseResult baseResult=new BaseResult();

        if (i>0){
       baseResult.setData(user1);
       baseResult.setCode(200);
       baseResult.setMessage("修改成功");

        }else {
         baseResult.setMessage("修改失败...");
         baseResult.setCode(500);
        }

        return baseResult;
    }

    @Override
    public BaseResult updateEmail(String email, HttpSession session) {
   com.qfedu.pojo.User user= (com.qfedu.pojo.User) session.getAttribute("user");

        Integer id=user.getId();
    int i=userMapper.updateEmai(email,id);
    com.qfedu.pojo.User u=userMapper.updateEmailById(id);

    BaseResult baseResult=new BaseResult();
    if (i>0){
        baseResult.setData(u);
        baseResult.setCode(200);
        baseResult.setMessage("修改成功");

    }else {
        baseResult.setMessage("修改失败");
        baseResult.setCode(500);
    }
        return baseResult;
    }

    @Override
    public BaseResult updatePassword(String password, HttpSession session) {
        com.qfedu.pojo.User user= (com.qfedu.pojo.User) session.getAttribute("user");
        Integer id=user.getId();

        int i=userMapper.updatePassword(password,id);
        com.qfedu.pojo.User u=userMapper.updatePasswordById(id);

        BaseResult baseResult=new BaseResult();

        if (i>0){
            baseResult.setCode(200);
            baseResult.setMessage("successful");
            baseResult.setData(u);


        }else {
            baseResult.setCode(500);
            baseResult.setMessage("defeat");

        }


        return baseResult;
    }
// 删除
    @Override
    public boolean delById(Integer id) {
        userMapper.delById(id);
        return true;
    }

    @Override
    public boolean insert(com.qfedu.pojo.User user) {
        return userMapper.insert(user)>0;
    }

    @Override
    public com.qfedu.pojo.User selectOne(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public BaseResult updateUser(com.qfedu.pojo.User user) {

        userMapper.updateUser(user);

        BaseResult baseResult= new BaseResult() ;
        baseResult.setCode(1);
        return baseResult;
    }


}
